package com.example.api.mall.logic.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.domain.user.User;
import com.example.entity.domain.user.UserLoginLog;
import com.example.entity.api.mall.user.LoginVo;
import com.example.entity.api.mall.user.RegisterVo;
import com.example.common.core.exception.LinkerException;
import com.example.api.mall.logic.IUserLogic;
import com.example.service.user.service.IUserLoginLogService;
import com.example.service.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.api.mall.utils.SmsUtils;
import com.example.api.mall.utils.PasswordUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Service
@Slf4j
public class UserLogicImpl implements IUserLogic {

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserLoginLogService userLoginLogService;

    @Override
    public User login(LoginVo loginVo, HttpServletRequest request) {
        User user1 = new User();
        user1 = this.loginWithUsername(loginVo);
        //修改最新登录信息
        User userSel = new User();
        userSel.setId(user1.getId());
        userSel.setLoginIp(request.getRemoteAddr());
        userSel.setLoginTime(new Date());
        userService.updateById(userSel);
        //记录登录日志
        UserLoginLog userLoginLog = new UserLoginLog();
        userLoginLog.setId(null);
        userLoginLog.setUserId(user1.getId());
        userLoginLog.setLoginIp(request.getRemoteAddr());
        userLoginLog.setLoginTime(new Date());
        userLoginLogService.save(userLoginLog);
        return user1;
    }

    private User loginWithUsername(LoginVo loginVo) {
        if (StrUtil.isBlank(loginVo.getUsername())){
            throw new LinkerException("用户名不能为空");
        }
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper.eq("username", loginVo.getUsername().trim());
        User user1 = userService.getOne(userWrapper);
        if (null == user1){
            throw new LinkerException("找不到该用户");
        }
        if (0 == user1.getOpenFlag()){
            throw new LinkerException("该用户当前处于锁定状态");
        }
        //如果密码不为空，则验证密码
        if (StrUtil.isNotBlank(loginVo.getPassword()) && (!PasswordUtils.verifyPassword(loginVo.getPassword(), user1.getPassword()))){
            throw new LinkerException("密码不正确");
        }
        //如果短信验证码不为空，则验证短信验证码
        if (StrUtil.isNotBlank(loginVo.getSmsCode()) && (!SmsUtils.verifySmsCode(loginVo.getUsername(), loginVo.getSmsCode()))){
            throw new LinkerException("短信验证码不正确");
        }
        return user1;
    }

    @Override
    public boolean register(RegisterVo registerVo, HttpServletRequest request) {
        User user = new User();
        user.setUsername(registerVo.getUsername());
        user.setPassword(PasswordUtils.encryptPassword(registerVo.getPassword()));
        user.setLoginIp(request.getRemoteAddr());
        user.setOpenFlag(1);
        user.setCreateTime(new Date());
        return userService.save(user);
    }

    @Override
    public User info(Long userId) {
        return userService.getById(userId);
    }
}
























